﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoginPanel : BasePanel
{
    private InputField idInput;
    private InputField pwInput;
    private Button loginBtn;
    private Button regBtn;

    public override void OnInit()
    {
        skinPath = "LoginPanel";
        layer = PanelManager.Layer.Panel;
    }

    public override void OnShow(params object[] para)
    {
        idInput = skin.transform.Find("IdInput").GetComponent<InputField>();
        pwInput = skin.transform.Find("PwInput").GetComponent<InputField>();
        loginBtn = skin.transform.Find("LoginBtn").GetComponent<Button>();
        regBtn = skin.transform.Find("RegisterBtn").GetComponent<Button>();
        
        loginBtn.onClick.AddListener(OnLoginClick);
        regBtn.onClick.AddListener(OnRegClick);
        
        NetManager.AddMsgListener("MsgLogin",OnMsgLogin);
        NetManager.AddEventListener(NetManager.NetEvent.ConnectSucc,OnConnectSucc);
        NetManager.AddEventListener(NetManager.NetEvent.ConnectFail,OnConnectFail);
        NetManager.Connect("127.0.0.1",8888);
    }


    public override void OnClose()
    {
        NetManager.RemoveMsgListener("MsgLogin",OnMsgLogin);
        
        NetManager.RemoveEventListener(NetManager.NetEvent.ConnectSucc,OnConnectSucc);
        NetManager.RemoveEventListener(NetManager.NetEvent.ConnectFail,OnConnectFail);
    }
    
    private void OnRegClick()
    {
        PanelManager.Open<RegisterPanel>();
    }

    private void OnLoginClick()
    {
        if (idInput.text==""||pwInput.text=="")
        {
            PanelManager.Open<TipPanel>("用户名和密码不能为空");
            return;
        }

        MsgLogin msgLogin = new MsgLogin();
        msgLogin.id = idInput.text;
        msgLogin.pw = pwInput.text;
        NetManager.Send(msgLogin);
    }
    
    
    private void OnMsgLogin(MsgBase msgbase)
    {
        MsgLogin msg = (MsgLogin)msgbase;
        if (msg.result==0)
        {
            Debug.Log("登录游戏");

            if (msg.result==0)
            {
                GameMain.id = msg.id;
                PanelManager.Open<RoomListPanel>();
                Close();
            }
        }
        else
        {
            PanelManager.Open<TipPanel>("账户或密码错误");
        }
    }
    
    private void OnConnectSucc(string err)
    {
        Debug.Log("OnConnectSucc");
    }
    
    private void OnConnectFail(string err)
    {
        Debug.Log("Fail");
    }
}
